package com.lxw;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MqConfig {

    private Map<String, Object> args = new HashMap<>();

    //声明队列
    @Bean
    public Queue queue(){
        return new Queue(MqConst.TEST_QUEUE, true, false, false, args);
    }

    //订阅模式
    @Bean("fanoutQueue1")
    public Queue queue2(){
        return new Queue(MqConst.FANOUT1, true, false, false, args);
    }
    @Bean("fanoutQueue2")
    public Queue queue3(){
        return new Queue(MqConst.FANOUT2, true, false, false, args);
    }



    //声明交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(MqConst.EXCHANGE_FANOUT);
    }
    //绑定交换机
    //将队列一绑定到交换机
    @Bean
    public Binding fanoutBinding1(FanoutExchange fanout, Queue fanoutQueue1) {
        return BindingBuilder.bind(fanoutQueue1).to(fanout);
    }
    //将队列二绑定到交换机
    @Bean
    public Binding fanoutBinding2(FanoutExchange fanout, Queue fanoutQueue2) {
        return BindingBuilder.bind(fanoutQueue2).to(fanout);
    }


    //路由模式
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(MqConst.EXCHANGE_DIRECT);
    }

    @Bean("info")
    public Queue queue4(){
        return new Queue(MqConst.EXCHANGE_DIRECT_INFO, true, false, false, args);
    }

    @Bean
    public Binding dirbinding1(DirectExchange directExchange, Queue info) {
        return BindingBuilder.bind(info).to(directExchange).with("info");
    }
    @Bean
    public Binding dirbinding2(DirectExchange directExchange, Queue info) {
        return BindingBuilder.bind(info).to(directExchange).with("error");
    }
    @Bean
    public Binding dirbinding3(DirectExchange directExchange, Queue info) {
        return BindingBuilder.bind(info).to(directExchange).with("warn");
    }


    //topic 模式
    @Bean("info_0")
    public Queue queue5(){
        return new Queue(MqConst.TOPIC_INFO0, true, false, false, args);
    }
    @Bean("info_1")
    public Queue queue7(){
        return new Queue(MqConst.TOPIC_INFO1,true, false, false, args);
    }
    @Bean("info_2")
    public Queue queue8(){
        return new Queue(MqConst.TOPIC_INFO2, true, false, false, args);
    }

    @Bean
    public TopicExchange topicExchange(){
        //声明交换机
        return new TopicExchange(MqConst.EXCHANGE_TOPIC);
    }

    //绑定交换机
    @Bean
    public Binding topicExchangeBind(Queue info_0){
        return BindingBuilder.bind(info_0).to(topicExchange()).with(MqConst.TOPIC_INFO0);
    }
    @Bean
    public Binding topicExchangeBind2(Queue info_1){
        return BindingBuilder.bind(info_1).to(topicExchange()).with(MqConst.TOPIC_INFO1);
    }
    @Bean
    public Binding topicExchangeBind3(Queue info_2){
        return BindingBuilder.bind(info_2).to(topicExchange()).with(MqConst.TOPIC_INFO2);
    }

}
